import argparse
import sys

def parse_args():
    parser = argparse.ArgumentParser("")
    parser.add_argument("--input", type=str,required=True)
    return parser.parse_args()

def calculate_median(array):
    sorted_array = sorted(array)
    length = len(sorted_array)

    if length % 2 == 0:
        # 如果数组长度为偶数，取中间两个数的平均值
        middle_left = sorted_array[length // 2 - 1]
        middle_right = sorted_array[length // 2]
        median = (middle_left + middle_right) / 2
    else:
        # 如果数组长度为奇数，取中间数
        median = sorted_array[length // 2]

    return median

def calculate_mean(array):
    total = sum(array)
    mean = total / len(array)
    return mean

def main():
    args=parse_args()
    # 读取数据文件
    # 打开原始数据文件和写入文件
    query_length=[]
    read_num=0
    with open(args.input, 'r') as input_file:
        # 创建一个集合来保存已经出现过的索引
        seen_indexes = set()
        
        # 逐行读取原始数据文件
        for line in input_file:
            # 以前两列作为索引
            #index = '||'.join(line.split()[:1])
            words = line.strip().split('\t')
            #if len(words)!=17:
            #    print('error!!!')
            #    continue
            read_num+=1
            query_length.append(len(words[3]))
    print('the basecalled successful read\' mean is {} and median is {}'.format(calculate_mean(query_length),calculate_median(query_length)))            
    print('sum of read: {}'.format(read_num))       


if __name__ == '__main__':
    sys.exit(main())